package cn.com.dhcc.turbo.service.registry;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.turbo.dao.registry.GapUserConfDao;
import cn.com.dhcc.turbo.entity.exch.GapUserConf;

@Service
public class GapUserConfService extends AbstractCrudService<GapUserConf> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.R);

	@Autowired
	private GapUserConfDao dao;
	

	@Autowired
	private NodeInfoService nodeInfoService;
	@Override
	public CrudDao<GapUserConf> getCrudDao() {
		return dao;
	}

	
	/**
	 * 默认网闸的设置
	 * @param conf
	 * @throws ServiceException 
	 */
    public void updateDefaultGap(String gapOwner) throws ServiceException{
    	WhereCondition condition=new WhereCondition();
    	List<GapUserConf> list=list(condition);
    	for (GapUserConf gapUserConf : list) {
			if (gapUserConf.getGapOwner().equals(gapOwner)) {
				gapUserConf.setStatus("1");
			}else{
				gapUserConf.setStatus("0");
			}
			update(gapUserConf);
		}
	}
   
    
    public GapUserConf getDefaultGap() throws ServiceException{
		WhereCondition condition=new WhereCondition();
		condition.where().eq("status", "1");
		List<GapUserConf> list=list(condition);
		if (list!=null && !list.isEmpty()) {
			return list.get(0);
		}else{
			return null;
		}
    }
	
	/**
	 * 获取网闸所有者节点编码
	 * 
	 * @return
	 */
	public List<GapUserConf> getUserConfList() throws NiceException {
		try {
			return dao.getUserConfList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	};
	
	/**
	 * 将gapOwner网闸设置为默认
	 * @param gapOwner
	 * @return
	 * @throws NiceException
	 */
	public int insertOrUpdataUserConf(String gapOwner) throws NiceException{
		try {
			List<GapUserConf> list=dao.list(new WhereCondition());
			for (GapUserConf gapUserConf : list) {
				String sql="select a.gap_owner from "+SI_TABLE("turbo_gap_user_conf")+" a,"+SI_TABLE("turbo_gap_owner_conf")+"b" +
						" where a.gap_owner==b.gap_owner and b.gap_owner=?";
				List<Map<String,Object>> listMaps=CM.getDao().listMap("", sql, new Object[]{gapUserConf.getGapOwner()});
				
				if ((listMaps==null || listMaps.isEmpty()) && gapUserConf.getGapOwner().equals(gapOwner)) {
					dao.delete(gapUserConf);//删除可选的网闸
					return 0;
				}else{
					if (gapUserConf.getGapOwner().equals(gapOwner) && listMaps!=null && !listMaps.isEmpty()) {//
						gapUserConf.setStatus("1");//设置为默认
						dao.update(gapUserConf);
					}else{
						if (listMaps!=null && !listMaps.isEmpty()) {
							gapUserConf.setStatus("0");//非默认
							dao.update(gapUserConf);
						}else{
							dao.delete(gapUserConf);//删除可选的网闸
						}
					}
				}
				
				
			}
			return 1;
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
		
		
	}
	
	public int deleteAll() throws ServiceException{
		try {
		
			return dao.deleteAll();
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 更新网闸所有者节点编码
	 * 
	 * @return
	 * @throws ServiceException 
	 */
	public List<GapUserConf> insertOrUpdataUserConfList(List<GapUserConf> dataList)
			throws ServiceException {
		try {
			String localNodeCode=nodeInfoService.getLocalNode().getNodeCode();
			return dao.insertOrUpdataUserConfList(dataList,localNodeCode);
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
	}

}
